<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 'loader.php';

class Spider{
    
    public $EE;
    public $campaigns;
    function __construct() {
        set_time_limit(0);
        $this->EE=& get_instance();
        
        $this->EE->load->library('api');
        $this->EE->api->instantiate('channel_entries');
    }
    
    function run(){
        $campaigns=$this->EE->db->get_where('spider_campaign', array('active'=>1));
        
        $this->campaigns=$campaigns->result_array();
        foreach ($this->campaigns as $campaign){
            $apis=unserialize($campaign['api']);
            foreach($apis as $api){
                $result=classLoader($api, $campaign, $this->EE);
                $data=$result->getData();
                
                $j=0;
                foreach ($data as $d){
                    //pr($d);
                    if($this->EE->api_channel_entries->submit_new_entry($campaign['channel_id'], $d)){
                        $j++;
                    }
                    if($j==$campaign['no_of_entry']){
                        break;
                    }
                }
            }
        }
        
    }
}


function pr($var){
    echo '<pre>';
    print_r($var);
    echo '</pre>';
}